/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.log4j.Logger;
import org.xeustechnologies.jtar.TarEntry;
import org.xeustechnologies.jtar.TarOutputStream;

/**
 *
 * @author Administrator
 */
public class TARUtil {

    public static Logger logger = Logger.getLogger(TARUtil.class.getName());

    public static void pack0(String tarPath, String srcFilePath) {
        long time = System.currentTimeMillis();
        try {
            TarArchiveOutputStream taos = new TarArchiveOutputStream(new FileOutputStream(tarPath));
            BufferedInputStream bis = null;
            try {
//            String offName = off.replaceAll(StringEscapeUtils.escapeJava(srcPath), "");
                TarArchiveEntry tae = new TarArchiveEntry(new File(srcFilePath).getName());
                tae.setSize(new File(srcFilePath).length());
                taos.putArchiveEntry(tae);
                bis = new BufferedInputStream(new FileInputStream(srcFilePath));
                int count;
                byte data[] = new byte[1024];
                while ((count = bis.read(data, 0, 1024)) != -1) {
                    taos.write(data, 0, count);
                }

                bis.close();
                taos.closeArchiveEntry();
            } catch (Exception e) {
                logger.info(e.getMessage(), e);
            } finally {
                try {
                    if (bis != null) {
                        bis.close();
                    }
                } catch (IOException e) {
                    logger.info(e.getMessage(), e);
                }
            }
            taos.close();
        } catch (Exception e) {
            logger.info(e.getMessage(), e);
        }
        System.out.println(System.currentTimeMillis() - time);
    }

    public static void pack(String tarPath, String srcFilePath) {
        TarOutputStream out = null;
        BufferedInputStream origin = null;
        try {
            out = new TarOutputStream(new BufferedOutputStream(new FileOutputStream(tarPath)));
            out.putNextEntry(new TarEntry(new File(srcFilePath), new File(srcFilePath).getName()));
            origin = new BufferedInputStream(new FileInputStream(new File(srcFilePath)));
            int count;
            byte data[] = new byte[2048];
            while ((count = origin.read(data)) != -1) {
                out.write(data, 0, count);
            }
            out.flush();
        } catch (Exception e) {
            logger.info("tar " + srcFilePath + " failed,cause " + e.getMessage(), e);
        } finally {
            try {
                if (origin != null) {
                    origin.close();
                }
            } catch (IOException e) {
                logger.info(e.getMessage(), e);
            }
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                logger.info(e.getMessage(), e);
            }
        }
    }
}
